package com.uduemc.biso.module.basic.service.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.github.pagehelper.page.PageMethod;
import com.uduemc.biso.core.extities.center.HostSetup;
import com.uduemc.biso.module.basic.service.mapper.HostSetupMapper;
import com.uduemc.biso.module.basic.service.service.HostSetupService;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
public class HostSetupServiceImpl implements HostSetupService {

	@Autowired
	private HostSetupMapper hostSetupMapper;

	@Override
	public HostSetup findInfoByHostId(Long hostId) {
		Example example = new Example(HostSetup.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("hostId", hostId);
		example.setOrderByClause("`id` ASC");
		List<HostSetup> selectByExample = hostSetupMapper.selectByExample(example);
		if (CollectionUtils.isEmpty(selectByExample))
			return null;
		return selectByExample.get(0);
	}

	@Override
	public HostSetup insert(HostSetup hostSetup) {
		hostSetupMapper.insertSelective(hostSetup);
		return findOne(hostSetup.getId());
	}

	@Override
	public HostSetup findOne(Long id) {
		return hostSetupMapper.selectByPrimaryKey(id);
	}

	@Override
	public HostSetup updateById(HostSetup hostSetup) {
		hostSetupMapper.updateByPrimaryKeySelective(hostSetup);
		return findOne(hostSetup.getId());
	}

	@Override
	public List<HostSetup> findAll(Pageable pageable) {
		int pageNumber = pageable.getPageNumber();
		int pageSize = pageable.getPageSize();
		PageMethod.startPage(pageNumber, pageSize);
		return hostSetupMapper.selectAll();
	}

	@Override
	public void deleteById(Long id) {
		hostSetupMapper.deleteByPrimaryKey(id);
	}

}
